package com.ruoyi.zd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zd.domain.InsurancePaymentRecords;
import com.ruoyi.zd.service.IInsurancePaymentRecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险缴费主记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-06
 */
@RestController
@RequestMapping("/zd/zd")
public class InsurancePaymentRecordsController extends BaseController
{
    @Autowired
    private IInsurancePaymentRecordsService insurancePaymentRecordsService;

    /**
     * 查询保险缴费主记录列表
     */
    @PreAuthorize("@ss.hasPermi('zd:zd:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsurancePaymentRecords insurancePaymentRecords)
    {
        startPage();
        List<InsurancePaymentRecords> list = insurancePaymentRecordsService.selectInsurancePaymentRecordsList(insurancePaymentRecords);
        return getDataTable(list);
    }

    /**
     * 导出保险缴费主记录列表
     */
    @PreAuthorize("@ss.hasPermi('zd:zd:export')")
    @Log(title = "保险缴费主记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsurancePaymentRecords insurancePaymentRecords)
    {
        List<InsurancePaymentRecords> list = insurancePaymentRecordsService.selectInsurancePaymentRecordsList(insurancePaymentRecords);
        ExcelUtil<InsurancePaymentRecords> util = new ExcelUtil<InsurancePaymentRecords>(InsurancePaymentRecords.class);
        util.exportExcel(response, list, "保险缴费主记录数据");
    }

    /**
     * 获取保险缴费主记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('zd:zd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(insurancePaymentRecordsService.selectInsurancePaymentRecordsById(id));
    }

    /**
     * 新增保险缴费主记录
     */
    @PreAuthorize("@ss.hasPermi('zd:zd:add')")
    @Log(title = "保险缴费主记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsurancePaymentRecords insurancePaymentRecords) {
        // 新增缴费记录
        int result = insurancePaymentRecordsService.insertInsurancePaymentRecords(insurancePaymentRecords);

        // 如果传入了employeeNumber，则更新员工保险方案
        if(result > 0 && insurancePaymentRecords.getEmployeeNumber() != null) {
            insurancePaymentRecordsService.updateEmployeeInsurancePlan(
                    insurancePaymentRecords.getEmployeeNumber(),
                    insurancePaymentRecords.getInsurancePlanId()
            );
        }

        return toAjax(result);
    }

    /**
     * 修改保险缴费主记录
     */
    @PreAuthorize("@ss.hasPermi('zd:zd:edit')")
    @Log(title = "保险缴费主记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsurancePaymentRecords insurancePaymentRecords)
    {
        return toAjax(insurancePaymentRecordsService.updateInsurancePaymentRecords(insurancePaymentRecords));
    }

    /**
     * 删除保险缴费主记录
     */
    @PreAuthorize("@ss.hasPermi('zd:zd:remove')")
    @Log(title = "保险缴费主记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(insurancePaymentRecordsService.deleteInsurancePaymentRecordsByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('zd:zd:list')")
    @GetMapping("/sums")
    public AjaxResult getPaymentSums(@RequestParam(required = false) String paymentMonth)
    {
        return success(insurancePaymentRecordsService.selectPaymentSums(paymentMonth));
    }
}
